/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.User;

/**
 *
 * @author Wei Liu
 */
public class UserDAO extends BaseEntityDAO
{

    public UserDAO()
    {
        // initialization
    }

    /**
     * Insert user into database
     * @param user the new user entity to be inserted
     * @return newly created user number or a 0 on error
     */
    public int insertUser(User user)
    {
        return save(user);
    }

    /**
     * Delete user that matches the supplied criteria
     * @param user the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteUser(User user)
    {
        return delete(user);
    }

    /**
     * Update the user using data from the user Data Object
     * @param user the user Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateUser(User user)
    {
        if (user.getUserID() == 0)
        {
            return false;
        }

        if (update(user))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the user that matches the supplied criteria
     * @param user the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public User findUser(User user)
    {
        String sql = null;

        if (user.getUserID() != 0 || (user.getUsername() != null && user.getPassword() != null))
        {
            sql = buildQuerySQL(user);
        }
        else
        {
            return null;
        }

        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (User) this.getObject(crs, user);
    }

    /**
     * Get all users that match the supplied criteria
     * @param user supplied criteria
     * @return a RowSet
     */
    public RowSet getUsersRS(User user)
    {
        String sql = buildQuerySQL(user);
        return query(sql);
    }

    /**
     * Get all users that match the supplied criteria
     * @param user supplied criteria
     * @return a collection of User Data Objects
     */
    public Collection<User> getUsersDO(User user)
    {
        String sql = buildQuerySQL(user);

        RowSet crs = query(sql);
        Collection<User> users = new ArrayList<User>();
        try
        {
            while (crs.next())
            {
                User userDO = (User) this.getObject(crs, user);
                users.add(userDO);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return users;
    }
}
